package com.qiu.greened.ui.main

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.core.content.edit
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.qiu.greened.R
import com.qiu.greened.databinding.FragmentMyBinding
import com.qiu.greened.network.ServiceCreator.BASE_URL
import com.qiu.greened.ui.my.LoginActivity
import com.qiu.greened.ui.post.PostUserActivity
import com.qiu.greened.utils.MyData

class MyFragment : Fragment() {

    private lateinit var myViewModel: MyViewModel
    private var _binding: FragmentMyBinding? = null
    private val binding get() = _binding!!
    private var save: SharedPreferences? = null


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentMyBinding.inflate(inflater, container, false)
        val root: View = binding.root
        myViewModel = ViewModelProvider(this).get(MyViewModel::class.java)
        save = activity?.getSharedPreferences("data", Context.MODE_PRIVATE)
        setViewData()
        binding.btnPost.setOnClickListener {
            val intent = Intent(activity, PostUserActivity::class.java)
            intent.putExtra("userId", MyData.userId)
            activity?.startActivity(intent)
        }
        binding.btnLogin.setOnClickListener {
            AlertDialog.Builder(requireActivity())
                .setTitle("退出")
                .setMessage("确定要退出吗?")
                .setCancelable(false)
                .setPositiveButton("确认") { _, _ ->
                    save?.edit {
                        putString("phone", "")
                        putString("password", "")
                        putInt("userId", 0)
                        putString("userName", "")
                        putString("userAvatar", "")
                        putString("token", "")
                    }
                    MyData.login = false
                    MyData.token = ""
                    MyData.userId = -1
                    Toast.makeText(activity, "已退出", Toast.LENGTH_SHORT).show()
                    startActivity(Intent(context, LoginActivity::class.java))
                    activity?.finish()
                }
                .setNegativeButton("取消") { _, _ -> }
                .show()

        }
        return root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    override fun onResume() {
        super.onResume()
        activity?.title = "我的"
        setViewData()
    }

    private fun setViewData() {
        binding.textLogin.text = "退出登录"
        binding.textName.text = save?.getString("userName", "")
        val userAvatar = save?.getString("userAvatar", "")
        Glide.with(this).load(BASE_URL + userAvatar)
            .apply(RequestOptions.bitmapTransform(CircleCrop())).into(binding.imgUser)
    }
}